#include <iostream>
#include <functional>
#include <vector>
#include <typeinfo>

typedef std::function<void(int, int)> EventHandler;

class EventManager {
public:
    void registerHandler(EventHandler handler) {
        handlers.push_back(handler);
    }

    // 触发事件
    void triggerEvent(int eventType, int eventCode) {
        std::cout << "Triggering event with type: " << eventType << " and code: " << eventCode << std::endl;
        for (auto& handler : handlers) {
            handler(eventType, eventCode);
        }
    }

    // 交换处理器
    void swapHandlers(size_t index1, size_t index2) {
        if (index1 < handlers.size() && index2 < handlers.size()) {
            std::cout << "Swapping handlers: " << index1 << " and " << index2 << std::endl;
            handlers[index1].swap(handlers[index2]);
        }
    }
    // 打印所有处理器的类型信息
    void printHandlersTypeInfo() {
        for (auto& handler : handlers) {
            std::cout << "Handler type info: " << handler.target_type().name() << std::endl;
        }
    }
private:
    std::vector<EventHandler> handlers;
};

void handleEvent(int type, int code) {
    std::cout << "Global function handling event with type: " << type << " and code: " << code << std::endl;
}

class Processor {
public:
    void process(int type, int code) {
        std::cout << "Processing event with type: " << type << " and code: " << code << std::endl;
    }
};

int main() {
    EventManager manager;
    Processor processor;

    manager.registerHandler(handleEvent);
    EventHandler globalFunctionHandler = handleEvent;
    EventHandler boundMemberFunc = std::bind(&Processor::process, &processor, std::placeholders::_1, std::placeholders::_2);
    manager.registerHandler(boundMemberFunc);

    int importantValue = 42;
    manager.registerHandler([importantValue](int type, int code) {
        std::cout << "Lambda handling event with type: " << type << " and code: " << code << " and important value: " << importantValue << std::endl;
    });

    manager.printHandlersTypeInfo();
    std::cout << "                       " << std::endl;
    // 判断是否绑定了 Processor::process
    auto targetMemberFunc = boundMemberFunc.target<void(Processor::*)(int, int)>();
    if (targetMemberFunc && *targetMemberFunc == &Processor::process) {
        std::cout << "The member function Processor::process is bound." << std::endl;
    } else {
        std::cout << "No matching target function bound for Processor::process." << std::endl;
    }

    // 判断是否绑定了 handleEvent
    auto targetGlobalFunc = globalFunctionHandler.target<void(*)(int, int)>();
    if (targetGlobalFunc && *targetGlobalFunc == handleEvent) {
        std::cout << "Global function handleEvent is bound." << std::endl;
    } else {
        std::cout << "No matching target function bound for handleEvent." << std::endl;
    }


    std:: cout << "                       " << std::endl;    
    manager.triggerEvent(11, 27);
   
    //交换顺序
    manager.swapHandlers(0, 1);
    std::cout << "after swap" << std::endl;
    std:: cout << "                       " << std::endl;
    manager.triggerEvent(11, 27);
}